<?php
/**
 * Created by PhpStorm.
 * User: xiaxiang
 * Date: 2019/3/15
 * Time: 10:11
 */

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = "category";
    public $timestamps = false;

    public static function getCategoryAll($pid = 0, $num = 0)
    {
        $arr = [];
        //根据pid找子分类
        $cols = self::where('pid', $pid)->get();
        $str = str_repeat('--', $num);
        $num++;
        foreach ($cols as $v) {
            $arr[] = [
                'id' => $v->id,
                'cname' => $v->cname,
                'indentcname' => $str . $v->cname,
                'recommend' => $v->recommend
            ];
            //外层数组默认产生下标
//            $sonCols = self::where('pid', $v->id)->get();
            $sonArr = self::getCategoryAll($v->id, $num);
            //拼接
            $arr = array_merge($arr, $sonArr);
        }
        return $arr;
    }
}